package com.dune.ajaxuo.shared;

import com.google.gwt.canvas.client.Canvas;
import com.google.gwt.canvas.dom.client.Context2d;
import com.google.gwt.canvas.dom.client.CssColor;
import com.google.gwt.dom.client.ImageElement;
import com.google.gwt.dom.client.Touch;
import com.google.gwt.event.dom.client.GestureStartEvent;
import com.google.gwt.event.dom.client.GestureStartHandler;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.TouchEndEvent;
import com.google.gwt.event.dom.client.TouchEndHandler;
import com.google.gwt.event.dom.client.TouchMoveEvent;
import com.google.gwt.event.dom.client.TouchMoveHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;

public class World {
	static final String url = "/sprites/cave.png";
	
	ImageElement imageElement = DOM.createImg().cast();
	//imageElement.setSrc(url);
	
	private Canvas canvas;
	private Context2d context;
	
	static final String holderId = "worldCanvas";
	static final String upgradeMessage = "Your browser does not support the HTML5 Canvas. Please upgrade your browser to run Ajax UO.";
	
	// mouse positions relative to canvas
	int mouseX, mouseY;
	
	// canvas size, in px
	static final int height = 400;
	static final int width = 400;
	
	final CssColor redrawColor = CssColor.make("rgba(0,0,0,0.6)");
	
	
	
	public World() {
		
		canvas = Canvas.createIfSupported();
		
		if (canvas == null) {
			RootPanel.get(holderId).add(new Label(upgradeMessage));
			return;
	    }
		
	    // init the canvases
	    canvas.setWidth(width + "px");
	    canvas.setHeight(height + "px");
	    canvas.setCoordinateSpaceWidth(width);
	    canvas.setCoordinateSpaceHeight(height);
	    RootPanel.get(holderId).add(canvas);
	    context = canvas.getContext2d();
		context.setFillStyle("rgb(0,0,0)");
	}
	
	public void update() {

	}
	
	public void draw() {

		context.fillRect(0, 0, width, height);
		context.drawImage(imageElement, 0, 0);
		
	}
	
	public void initHandlers() {
	    canvas.addMouseMoveHandler(new MouseMoveHandler() {
	      public void onMouseMove(MouseMoveEvent event) {
	        mouseX = event.getRelativeX(canvas.getElement());
	        mouseY = event.getRelativeY(canvas.getElement());
			context.setFillStyle("rgb(255,0,0)");
	      }
	    });
	    
	    canvas.addMouseOutHandler(new MouseOutHandler() {
	      public void onMouseOut(MouseOutEvent event) {
	        mouseX = -200;
	        mouseY = -200;
			context.setFillStyle("rgb(0,0,0)");
	      }
	    });

	    canvas.addTouchMoveHandler(new TouchMoveHandler() {
	      public void onTouchMove(TouchMoveEvent event) {
	        event.preventDefault();
	        if (event.getTouches().length() > 0) {
	          Touch touch = event.getTouches().get(0);
	          mouseX = touch.getRelativeX(canvas.getElement());
	          mouseY = touch.getRelativeY(canvas.getElement());
	        }
	        event.preventDefault();
	      }
	    });

	    canvas.addTouchEndHandler(new TouchEndHandler() {
	      public void onTouchEnd(TouchEndEvent event) {
	        event.preventDefault();
	        mouseX = -200;
	        mouseY = -200;
	      }
	    });

	    canvas.addGestureStartHandler(new GestureStartHandler() {
	      public void onGestureStart(GestureStartEvent event) {
	        event.preventDefault();
	      }
	    });
	  }
	
}


